<div class="partie">
	<div class="advPartie" title="Ajouter une question" onclick="accordion('<?php echo $GLOBALS["siteUrl"]?>','addQuestion')">
		<img id="btnaddQuestion" class="btnAccordion" src="/trivia/ressource/openJoin.png"/>
	 	<span class="textPartie">Ajouter une question </span>
	</div>
	<div id="accordion-contentaddQuestion" class="accordion">
		<div class="Important">Remplissez le formulaire ci-dessous pour ajouter une question</div><br>
		<div class="CSSTableGenerator">
			<form id="form_addQuestion">
	    		<select name="domaine" title="Choisissez le domaine">
					<?php
					foreach ($data["domaines"] as $domaine){ //Select des domaines
					?>
						<option name="<?php echo $domaine->getId();?>" value="<?php echo $domaine->getId();?>"><?php echo $domaine->getLibelle();?></option>
					<?php 
					}
					?>
				</select>		      
				<input type="text" name="libelle" required="required" placeholder="Saississez votre question"/>
				<input type="text" name="repValid" required="required" placeholder="Saississez la bonne réponse"/>
				<input type="text" name="rep1" required="required" placeholder="Saississez une 1ère réponse"/>
				<input type="text" name="rep2" required="required" placeholder="Saississez une 2ème réponse"/>
				<input type="text" name="rep3" required="required" placeholder="Saississez une 3ème réponse"/>		
				<input type="submit" id="ajouterQuestion" name="ajouterQuestion" value="Ajouter"/>
			</form>
		</div>
	</div>

	<div class="advPartie" title="Afficher mes questions" onclick="accordion('<?php echo $GLOBALS["siteUrl"]?>','displayQuestions')">
		<img id="btndisplayQuestions" class="btnAccordion" src="/trivia/ressource/openJoin.png"/>
	 	<span class="textPartie">Afficher les questions</span>
	</div>
	<div id="accordion-contentdisplayQuestions" class="accordion">
		<?php 
		$i=0;
		foreach ($data["result"] as $result){ //Select des domaines
			$domaine=$result["domaine"];
			$questions=$result["questions"];
		?>
		<div class="advPartie" title="Mes questions du domaine <?php echo $domaine->getLibelle();?>" onclick="accordion('<?php echo $GLOBALS["siteUrl"]?>','displayQuestions<?php echo $i;?>')">
			<img id="btndisplayQuestions<?php echo $i;?>" class="btnAccordion" src="/trivia/ressource/openJoin.png"/>
		 	<span class="textPartie">Voici vos questions pour le domaine <?php echo $domaine->getLibelle();?></span>
		</div>
		<div id="accordion-contentdisplayQuestions<?php echo $i;?>" class="accordion">
			<div class="CSSTableGenerator">
				<table>
					<tr>
						<td>Question</td>
						<td>Actions</td>			
					</tr>
					<?php 
						foreach ($questions as $question){ //Select des questions selon les domaines
					?>
					<tr id="question<?php echo $question->getId();?>">					
						<td><?php echo $question->getLibelle();?></td>
						<td id="tdActionQuestions">							
							<img class="updateQuestion" id="update<?php echo $question->getId();?>" src="/trivia/ressource/update-mark.png" title="Modifier" alt="Modifier <?php echo $question->getLibelle();?>"/>
							<img class="deleteQuestion" id="delete<?php echo $question->getId();?>" src="/trivia/ressource/x-mark.png" title="Supprimer" alt="Supprimer <?php echo $question->getLibelle();?>"/>
						</td>
					</tr>
					<?php 								
						}
					?>
				</table>
			</div>
		</div>
		<?php
			$i++;
		}
		?>
	</div>
	<script type="text/javascript">
		$(function(){
			
			$(".updateQuestion").click(function(e){
				e.preventDefault();
				e.stopPropagation();
				var id=$(this).attr("id").replace("update",""),
					url="/trivia/CQuestion/loadEdit/"+id;
				$.ajax({
					type: "get",
					url: url,
					success: function(data){
						var result=$.parseJSON(data);
						$("#form_addQuestion").putForm(result);
					}
				});
			});
			
			$(".deleteQuestion").click(function(e){
				e.preventDefault();
				e.stopPropagation();
				var id=$(this).attr("id").replace("delete",""),
					url="/trivia/CQuestion/delete/"+id;
				$.ajax({
					type: "get",
					url: url,
					success: function(data){
						var result=$.parseJSON(data);
						if(result.valid){
							$("#question"+id).remove();
						}
					}
				});
			});
			
		});
		
		$.fn.putForm=function(question){
			var form=$(this);
			console.log(question);
			form.find("select").val(question.domaine);
			form.find("input[name='libelle']").val(question.libelle);
			form.find("input[name='repValid']").val(question.repValid);
			form.find("input[name='rep1']").val(question.rep1);
			form.find("input[name='rep2']").val(question.rep2);
			form.find("input[name='rep3']").val(question.rep3);
			form.find("input[type='submit']").val("Modifier");
		};
	</script>